﻿// For an introduction to the Blank template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232509
(function () {
    "use strict";

    WinJS.Binding.optimizeBindingReferences = true;

    var app = WinJS.Application;
    var activation = Windows.ApplicationModel.Activation;

    app.onactivated = function (args) {
        if (args.detail.kind === activation.ActivationKind.launch) {
            if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                // TODO: This application has been newly launched. Initialize
                // your application here.
            } else {
                // TODO: This application has been reactivated from suspension.
                // Restore application state here.
            }
            args.setPromise(WinJS.UI.processAll().then(function () {

                var source = document.getElementById("source");
                var canvas = document.createElement("canvas");
                canvas.width = source.clientWidth;
                canvas.height = source.clientHeight;
                var context = canvas.getContext("2d");

                context.drawImage(source, 0, 0);

                var blob = canvas.msToBlob();

                var imageManipulator = new DeForm.ImageManipulator(blob.msDetachStream());

                var convolve = new DeForm.ColorMatrixEffectDescription();
                convolve.colorMatrix = [0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0,0.0, 0.0, 0.0, 0.0];

                imageManipulator.addEffectDescription(convolve);

                imageManipulator.manipulate(false);

                var inMemoryRandomAccessStream = new Windows.Storage.Streams.InMemoryRandomAccessStream();

                imageManipulator.saveToStream(inMemoryRandomAccessStream, DeForm.CompressionMode.png);

                var blob = window.MSApp.createBlobFromRandomAccessStream("image/png", inMemoryRandomAccessStream);

                var url = URL.createObjectURL(blob, { oneTimeOnly: true });
                document.getElementById("destination").src = url;
            }));
        }
    };

    app.oncheckpoint = function (args) {
        // TODO: This application is about to be suspended. Save any state
        // that needs to persist across suspensions here. You might use the
        // WinJS.Application.sessionState object, which is automatically
        // saved and restored across suspension. If you need to complete an
        // asynchronous operation before your application is suspended, call
        // args.setPromise().
    };

    app.start();
})();
